package com.stockz.scanner.screen;

import java.util.List;

import com.stockz.core.model.Bar;
import com.stockz.core.util.BarUtil;
import com.stockz.core.util.BarUtil.BarValueType;
import com.stockz.scanner.screen.configuration.GainerScreenConfiguration;

public class GainerScreen implements MatchStrengthCalculatingScreen<GainerScreenConfiguration> {

	private double matchStrength;
	private BarValueType barValueType = BarValueType.AVGPRICE;
	
	private final GainerScreenConfiguration configuration = new GainerScreenConfiguration();
	
	@Override
	public GainerScreenConfiguration getConfiguration() {
		return configuration;
	}
	
	@Override
	public double getMatchStrength() {
		return matchStrength;
	}
	
	@Override
	public int getMandatorySpan() {
		return configuration.getSpan();
	}
	
	@Override
	public boolean isMatch(List<Bar> barList) {
		
		if(barList == null || barList.size() <= configuration.getSpan()){
			return false;
		}
		
		Bar highest = barList.get(0);
		Bar lowest = barList.get(0);
		
		for(int i = 0; i < configuration.getSpan(); i++){
			if(BarUtil.getValue(highest,barValueType) < BarUtil.getValue(barList.get(i),barValueType)){
				highest = barList.get(i);
			}
			
			if(BarUtil.getValue(lowest,barValueType) > BarUtil.getValue(barList.get(i),barValueType)){
				lowest = barList.get(i);
			}
		}
		if(highest.getDate().after(lowest.getDate())){
			double low = BarUtil.getValue(lowest,barValueType);
			if(low != 0){
				double high = BarUtil.getValue(highest,barValueType);
				double gain = (high - low)*100/low;
				if(gain >= configuration.getGainPercentage()){
					matchStrength = gain;
					return true;									
				}
			}
		}
		return false;
	}
}
